package net.cathackers.devel.scmp.auth.impl;

import java.util.HashSet;
import java.util.Random;

public class UniqueNumberGenerator {
    private HashSet<Long> generatedNumbersCache_;
    private Random gen_;

    public UniqueNumberGenerator() {
	generatedNumbersCache_ = new HashSet<Long>();
	gen_ = new Random(System.currentTimeMillis());
    }

    public synchronized void reclaimNumber(long number) {
	generatedNumbersCache_.remove(number);
    }

    public synchronized long newUniqueLongNumber() {
	long ret = 0;
	do {
	    ret = gen_.nextLong();
	} while (generatedNumbersCache_.contains(ret));
	generatedNumbersCache_.add(ret);
	return ret;
    }
    
    public synchronized void reset() {
	generatedNumbersCache_.clear();
    }
}
